package com.lj.universalframework.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.lj.universalframework.R
import com.lj.universalframework.utils.Utils
import com.lj.universalframework.utils.clickWithTriggerS
import java.util.concurrent.atomic.AtomicInteger


/**
 * <p>文件名：AbnormalStateView
 * <p>创建者：LJ
 * <p>创建日期：2020\10\19 0019 15:37
 * <p>描述：自定义异常视图
 */
class LJAbnormalStateView : ConstraintLayout {
    //当前状态 0---》加载中 1---》网络不通 2---》网络通了，但请求异常 3---》网络通，但请求无数据  4--》
    private var currentState = AtomicInteger(0)

    //网络异常提示内容
    private var networkAnomaly = "诶哎，怎么没网了！"

    //请求异常提示内容
    private var requestException = "服务器异常..."

    //无数据提示内容
    private var noData = "空空如也也是一种态度"

    //网络异常提示图片
    private var networkAnomalySrc: Int = 0

    //请求异常提示图片
    private var requestExceptionSrc: Int = 0

    //无数据提示图片
    private var noDataSrc: Int = 0

    //无数据时否显示刷新按钮
    private var noDataButtonDisplay: Boolean = false

    //无数据时刷新按钮显示文字(true)还是图片(false)
    private var noDataButtonTextAndImg: Boolean = true

    //无数据时刷新按钮显示图片资源
    private var noDataButtonImg: Int = 0
    //异常时否显示刷新按钮（所有异常：网络+请求）
    private var requestButtonDisplay:Boolean=false
    //异常时刷新按钮显示文字(true)还是图片(false)
    private var requestButtonTextAndImg: Boolean = true
    //异常时否显示刷新按钮时按钮文字
    private var requestButtonText = "刷新"


    //文字内容控件
    private lateinit var tvAsv: TextView

    //图片内容控件
    private lateinit var ivAsv: ImageView

    //刷新图片控件
    private lateinit var ivRefresh: ImageView

    //刷新文本控件
    private lateinit var tvRefresh: TextView

    //加载中效果
    private lateinit var llLoad: LinearLayout

    //异常布局
    private lateinit var abnormalLayout: LinearLayout

    //刷新回调
    var refresh: ((currentState:Int) -> Unit)? = null


    constructor(context: Context) : super(context) {
        initView(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context, attrs)
    }

    /**
     * 初始化组件
     *
     * @param context
     */
    private fun initView(context: Context, attrs: AttributeSet?) {
        val abnormalState = LayoutInflater.from(context).inflate(R.layout.abnormal_state_view, this)
        tvAsv = abnormalState.findViewById(R.id.tv_asv)
        ivAsv = abnormalState.findViewById(R.id.iv_asv)
        abnormalLayout = abnormalState.findViewById(R.id.ll_null_view)
        llLoad = abnormalState.findViewById(R.id.ll_load)
        ivRefresh = abnormalState.findViewById(R.id.iv_refresh)
        tvRefresh = abnormalState.findViewById(R.id.tv_refresh)
        //刷新回调
        arrayListOf(ivRefresh,tvRefresh).clickWithTriggerS {
            refresh?.invoke(currentState.get())
        }

        if (attrs != null) {
            val aS = context.obtainStyledAttributes(attrs, R.styleable.LJAbnormalStateView)
            //网络异常提示内容
            val nA = aS.getString(R.styleable.LJAbnormalStateView_networkAnomaly)
            if (nA != null) {
                networkAnomaly = nA
            }
            //请求异常提示内容
            val rE = aS.getString(R.styleable.LJAbnormalStateView_requestException)
            if (rE != null) {
                requestException = rE
            }

            //无数据提示内容
            val nD = aS.getString(R.styleable.LJAbnormalStateView_noData)
            if (nD != null) {
                noData = nD
            }

            //网络异常提示图片,无网络
            networkAnomalySrc = aS.getResourceId(
                R.styleable.LJAbnormalStateView_requestExceptionSrc,
                R.mipmap.no_network
            )
            //请求异常提示图片，服务器异常
            requestExceptionSrc = aS.getResourceId(
                R.styleable.LJAbnormalStateView_requestExceptionSrc,
                R.mipmap.server_exception
            )
            //无数据提示图片
            noDataSrc =
                aS.getResourceId(R.styleable.LJAbnormalStateView_noDataSrc, R.mipmap.no_data)
            //无数据时否显示刷新按钮
            noDataButtonDisplay =
                aS.getBoolean(R.styleable.LJAbnormalStateView_noDataButtonDisplay, false)
            //无数据时刷新按钮显示文字(true)还是图片(false)
            noDataButtonTextAndImg =
                aS.getBoolean(R.styleable.LJAbnormalStateView_noDataButtonTextAndImg, true)
            //无数据时刷新按钮显示图片资源
            noDataButtonImg = aS.getResourceId(
                R.styleable.LJAbnormalStateView_requestExceptionSrc,
                R.mipmap.no_data
            )
            //异常时否显示刷新按钮（所有异常：网络+请求）
            requestButtonDisplay=aS.getBoolean(R.styleable.LJAbnormalStateView_requestButtonDisplay,false)
            //异常时刷新按钮显示文字(true)还是图片(false)
            requestButtonTextAndImg=aS.getBoolean(R.styleable.LJAbnormalStateView_requestButtonTextAndImg,true)
            //异常时否显示刷新按钮时按钮文字
            requestButtonText=aS.getString(R.styleable.LJAbnormalStateView_requestButtonText)?:"刷新"
        } else {
            networkAnomalySrc = R.mipmap.no_network
            requestExceptionSrc = R.mipmap.server_exception
            noDataSrc =  R.mipmap.no_data
        }
        //检查网络状态
        if (Utils.isNetworkConnected(context)) {
            modifyStatus(0)
        } else {
            modifyStatus(3)
        }
    }

    /**
     * 修改状态
     */
    fun modifyStatus(newType: Int) {
        if (currentState.get() == newType) return
        currentState.set(newType)
        if (newType == 0) {
            llLoad.visibility = VISIBLE
            abnormalLayout.visibility = GONE
            tvAsv.text = networkAnomaly
            ivAsv.setImageResource(networkAnomalySrc)
            return
        }
        llLoad.visibility = GONE
        abnormalLayout.visibility = VISIBLE
        when (newType) {
            1 -> {
                //网络不通
                tvAsv.text = networkAnomaly
                ivAsv.setImageResource(networkAnomalySrc)
                ivRefresh.visibility = GONE
                if (requestButtonDisplay){
                    if (requestButtonTextAndImg){
                        tvRefresh.visibility = VISIBLE
                        tvRefresh.text = requestButtonText
                    }else{
                        ivRefresh.visibility = GONE
                    }
                }else{
                    ivRefresh.visibility = GONE
                    tvRefresh.visibility = GONE
                }
            }

            2 -> {
                //网络通了，但请求异常
                tvAsv.text = requestException
                ivAsv.setImageResource(requestExceptionSrc)
                if (requestButtonDisplay){
                    if (requestButtonTextAndImg){
                        tvRefresh.text = requestButtonText
                        tvRefresh.visibility = VISIBLE
                    }else{
                        ivRefresh.visibility = GONE
                    }
                }else{
                    ivRefresh.visibility = GONE
                    tvRefresh.visibility = GONE
                }
            }

            3 -> {
                //网络通，但请求无数据
                tvAsv.text = noData
                ivAsv.setImageResource(noDataSrc)
                if (noDataButtonDisplay) {
                    if (noDataButtonTextAndImg) {
                        ivRefresh.visibility = GONE
                        tvRefresh.visibility = VISIBLE
                    } else {
                        ivRefresh.visibility = VISIBLE
                        tvRefresh.visibility = GONE
                        ivRefresh.setImageResource(noDataButtonImg)
                    }
                }
            }
        }
    }
}